/*
String to Integer (atoi)


Implement atoi to convert a string to an integer.

Hint: Carefully consider all possible input cases. If you want a challenge, 
please do not see below and ask yourself what are the possible input cases.

Notes: It is intended for this problem to be specified vaguely (ie, no given input specs).
You are responsible to gather all the input requirements up front.

Update (2015-02-10):
The signature of the C++ function had been updated. If you still see your function signature accepts a const char * argument, 
please click the reload button  to reset your code definition.
*/

#include <iostream>
#include <vector>
#include <math.h>
using namespace std;

class Solution {
public:
	int atoi(string str) {
		int len = str.length();
		int result = 0;
		int sign = 1;
		int weight = 0;
		int i = 0;

		while (str[i] == ' ' && i < len)i++;

		if (str[i] == '+')
		{
			i++;
		}
		else if(str[i] == '-'){
			sign = -1;
			i++;
		}
		
		for (; i < len; i++)
		{
			if (str[i] < '0' || str[i] > '9')
			{
				break;
			}
			if (result > INT_MAX / 10 || 
				(result == INT_MAX / 10 && (str[i] - '0') > INT_MAX % 10))
			{
				return sign == -1 ? INT_MIN : INT_MAX;
			}
			result = result * 10 + str[i] - '0';
		}

		return sign * result;
	}
};

void strToCharPtr()
{
	//const char
	string s1 = "abcdeg";
	const char *k = s1.c_str();
	const char *t = s1.data();

	cout << k<<" "<< t <<endl;

	//
	
	
	char *data;
	//data = (char *)malloc((len + 1) * sizeof(char));
	//s1.copy(data, len, 0);
	data = const_cast<char *>(s1.data());
	cout << data << endl;

	string str2("1234");
	for (size_t i = 0; i < str2.size(); i++)
	{
		cout << str2[i]<< endl;
	}


}

int main(int argc, char *argv[])
{
	int i;
	int a;

	//strToCharPtr();
	string str;


	Solution s;




	for (i = 1; i < argc; i++){
		cout << argv[i] << endl;
		//a = atoi(argv[i]);
		str.assign(argv[i], strlen(argv[i]));
		cout << s.atoi(str)<< endl;


	}

	//1000000003
	//cout << sizeof(int) << endl;
	//cout << INT_MIN << endl;
	//cout << "Input interg: " << a << endl;
	//Solution s;
	//cout << "Reverse interg: " << s.reverse(a) << endl;
	//cin >> i;

	system("pause");
	return 0;

}